﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Text;
using CSS.Common;
using CSS.Action;
using Thuoc24h.Data;

namespace CSS
{
    public static class Utils
    {
        public static string RemoveUnicode(string text)
        {
            string[] arr1 = new string[] { "á", "à", "ả", "ã", "ạ", "â", "ấ", "ầ", "ẩ", "ẫ", "ậ", "ă", "ắ", "ằ", "ẳ", "ẵ", "ặ",  
        "đ",  
        "é","è","ẻ","ẽ","ẹ","ê","ế","ề","ể","ễ","ệ",  
        "í","ì","ỉ","ĩ","ị",  
        "ó","ò","ỏ","õ","ọ","ô","ố","ồ","ổ","ỗ","ộ","ơ","ớ","ờ","ở","ỡ","ợ",  
        "ú","ù","ủ","ũ","ụ","ư","ứ","ừ","ử","ữ","ự",  
        "ý","ỳ","ỷ","ỹ","ỵ",};
            string[] arr2 = new string[] { "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",  
        "d",  
        "e","e","e","e","e","e","e","e","e","e","e",  
        "i","i","i","i","i",  
        "o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o",  
        "u","u","u","u","u","u","u","u","u","u","u",  
        "y","y","y","y","y",};
            for (int i = 0; i < arr1.Length; i++)
            {
                text = text.Replace(arr1[i], arr2[i]);
                text = text.Replace(arr1[i].ToUpper(), arr2[i].ToUpper());
            }
            return text;
        }
        public static string ToUnsignName(string text)
        {
            text = Utils.RemoveUnicode(text);
            text = Utils.ToSeoFriendly(text, 200);
            return text;
        }
        public static string ToSeoFriendly(string title, int maxLength)
        {
            var match = Regex.Match(title.ToLower(), "[\\w]+");
            StringBuilder result = new StringBuilder("");
            bool maxLengthHit = false;
            while (match.Success && !maxLengthHit)
            {
                if (result.Length + match.Value.Length <= maxLength)
                {
                    result.Append(match.Value + "-");
                }
                else
                {
                    maxLengthHit = true;
                    if (result.Length == 0) result.Append(match.Value.Substring(0, maxLength));
                }
                match = match.NextMatch();
            }
            if (result[result.Length - 1] == '-') result.Remove(result.Length - 1, 1);
            return result.ToString();
        }
        public static string BuildSeoLink(object unsignUrl)
        {
            return unsignUrl.ToString().ToLower() + AppSettings.GetString(Constants.URL_EXTENTION);
        }
        public static string BuildUrl(object name, object id = null)
        {
            if (id == null)
            {
                return BuildSeoLink(ToUnsignName(name.ToString()));
            }
            else
            {
                return BuildSeoLink(ToUnsignName(name.ToString()) + "-" + id);
            }
        }
        public static string BuildVoteMark(object total = null, object voteCount = null)
        {
            string html = "<div class=\"VoteItem\"><div class=\"VoteValue\" style=\"width:{0}%\"></div></div>";
            if (total == null || voteCount == null)
            {
                html = string.Format(html, 100);
            }
            else
            {
                double _total = int.Parse(total.ToString());
                double _count = int.Parse(voteCount.ToString());
                if (_total == 0)
                {
                    _total = 5;
                }
                if (_count == 0)
                {
                    _count = 1;
                }
                var percent = ((_total / _count) / 5) * 100;
                html = string.Format(html, percent);
            }
            return html;
        }
        public static string GetYoutubeVideoThumb(object videoUrl)
        {
            Regex reg = new Regex(@"youtu(?:\.be|be\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)");
            Match youtubeMatch = reg.Match(videoUrl + "");
            string id = string.Empty;
            if (youtubeMatch.Success)
                id = youtubeMatch.Groups[1].Value;
            return string.Format("http://img.youtube.com/vi/{0}/mqdefault.jpg", id);
        }
        public static string GetOsIconUrl(object osName)
        {
            return "/Home/theme/images/" + ToSeoFriendly(RemoveUnicode(osName.ToString()), 100) + ".png";
        }
        public static string GetConfig(string key)
        {
            MedLifeDataContext ct = new MedLifeDataContext();
            var config = ct.Configs.FirstOrDefault(it => it.Name.ToLower() == key.ToLower());
            if (config != null)
            {
                return config.Data;
            }
            else
            {
                return "";
            }
        }
        public static string FormatMoney(this object moneyString, string defaultValue = "Liên hệ")
        {
            if (moneyString != null)
            {
                if (moneyString.ToString() == "0")
                {
                    return defaultValue;
                }
                else
                {
                    return Double.Parse(moneyString.ToString()).ToString("#,###").Replace(",", ".") + " đ";
                }
            }
            else
            {
                return "0";
            }
        }
    }
}